<?php

/**
 * Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
 * Copyright (c) 2010 Mr. Ynk: http://www.mrynk.nl
 */

/**
 * @author Michel Ypma: michel@mrynk.nl
 */

require_once 'interfaces/iClassBuilder.php';
require_once 'BuilderUtils.php';
require_once 'Syntax.php';


class BaseClassBuilder implements iClassBuilder
{
    private $language;
    private $path;
    private $class;
    private $s_root;


    public function __construct($class)
    {
        $this->language     = $class['language'];
        $this->path         = $class['path'];
        $this->class        = $class['class'];

        $this->buildClass();
        $this->buildAttributes();
        $this->buildMethods();
    }

    private function buildClass()
    {
        $this->s_root = new Syntax('package', $this->language);
        $this->s_root->set('class_path', $this->path.'.om');

        $s_class = $this->s_root->newChild('class');
        $s_class->set('visibility', 'public');
        $s_class->set('class', 'Base'.BuilderUtils::className($this->class['name']));

        $this->s_root->addChild($s_class);
    }

    private function buildAttributes()
    {
        $s_class = $this->s_root->getChild('class');

        foreach ($this->class->attribute as $attribute)
        {
            $s_attr = $s_class->newChild('attribute', $attribute);
            $s_attr->set('visibility', 'public');
            $s_attr->set('attribute', BuilderUtils::attributeName($attribute['name']));
            $s_attr->set('type', $attribute['type']);

            $s_class->addChild($s_attr);
        }
    }

    private function buildMethods()
    {
        $s_class = $this->s_root->getChild('class');

        foreach ($this->class->attribute as $attribute)
        {
            if ($attribute['getter'] != 'false')
            {
                $s_getter = $s_class->newChild('getter', $attribute);
                $s_getter->set('visibility',        'public');
                $s_getter->set('method',            BuilderUtils::methodName($attribute['name']));
                $s_getter->set('type',              $attribute['type']);
                $s_getter->set('attribute',         BuilderUtils::attributeName($attribute['name']));

                $s_class->addChild($s_getter);
            }

            if ($attribute['setter'] != 'false')
            {
                $s_setter = $s_class->newChild('setter', $attribute);
                $s_setter->set('visibility',        'public');
                $s_setter->set('method',            BuilderUtils::methodName($attribute['name']));
                $s_setter->set('type',              $attribute['type']);
                $s_setter->set('attribute',         BuilderUtils::attributeName($attribute['name']));

                $s_class->addChild($s_setter);
            }
        }
    }

    public function write()
    {
        $contents = BuilderUtils::indent($this->s_root->getContents());

        foreach ($this->language as $language)
        {
            BuilderUtils::writeFile($language['target'].str_replace('.', '/', $this->path).'/om/Base'.BuilderUtils::className($this->class['name']).'.'.$language['name'], $contents[(string) $language['name']]);
        }
    }
}
?>